﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Enums;

namespace Yglu.BusinessLayer.Entities
{
	public class Order : BaseEntity
	{
		#region Properties

		public PaymentType PaymentType { get; set; }
		public String Customer { get; set; }
		public Double TotalPrice { get; set; }
		public Double Discount { get; set; }
		public DateTime DateOrdered { get; set; }
		public DateTime DatePaid { get; set; }

		public Terminal Terminal { get; set; }
		public User User { get; set; }
		public ComboCollection Combos { get; set; }
		public Promotion PromotionApplied { get; set; }

		#endregion

		#region Extra properties

		public Double PriceWithDiscount
		{
			get
			{
				Double price = Combos.PriceWithDiscount;

				if (PromotionApplied != null)
				{
					if (PromotionApplied.IsPercent)
					{
						return price - (price * (PromotionApplied.Discount / 100));
					}

					return price - PromotionApplied.Discount;
				}

				return price;
			}
		}

		public String StoreTitle
		{
			get
			{
				return Terminal.Store.Title;
			}
		}

		public String UserFullName
		{
			get
			{
				return User.FullName;
			}
		}

		#endregion

		#region Constructor

		public Order()
		{
			User = new User();
			Combos = new ComboCollection();
		}

		public Order(Int32 id)
		{
			Construct(DataAccess.Order_Get(id));
		}

		public Order(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Methods

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "OrderId");
			PaymentType = (PaymentType)DataHandler.GetInt32(reader, "OrderPaymentType");
			Customer = DataHandler.GetString(reader, "OrderCustomer");
			TotalPrice = DataHandler.GetDouble(reader, "OrderTotalPrice");
			Discount = DataHandler.GetDouble(reader, "OrderDiscount");
			DateOrdered = DataHandler.GetDateTime(reader, "OrderDateOrdered");
			DatePaid = DataHandler.GetDateTime(reader, "OrderDatePaid");
			Active = DataHandler.GetBoolean(reader, "OrderActive");

			Terminal = new Terminal();
			Terminal.Load(reader);

			User = new User();
			User.Load(reader);

			Combos = new ComboCollection();
		}

		#endregion
	}
}